package com.jhgsys.internal.sypq.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.DateUtil;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.sypq.entity.ModelAttach;
import com.jhgsys.internal.sypq.entity.ModelOars;
import com.jhgsys.internal.sypq.params.OarsAddParams;
import com.jhgsys.internal.sypq.service.IModelOarsService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.Random;

/**
 * 桨模 Controller
 *
 * @author ganjun
 * @date 2024-11-18 22:28:43
 */
@Slf4j
@Validated
@Controller
public class ModelOarsController extends BaseController {

    @Autowired
    private IModelOarsService modelOarsService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelOars")
    @RequiresPermissions("modelOars:view")
    public ModelAndView modelOarsIndex(@RequestParam(name="projectName", required=false, defaultValue="")String projectName){
        String viewPath = jumpPage("sypq/modelOars/modelOarsList");
        ModelAndView modelAndView = new ModelAndView(viewPath);
        modelAndView.addObject("projectName", projectName);
        return modelAndView;
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelOars/add")
    @RequiresPermissions("modelOars:add")
    public String modelOarsAdd(Model model){
        String dateStr = DateUtil.getDateFormat(new Date(), "yyyyMM");
        Random random = new Random();
        int randomNumber = random.nextInt(10000); // 生成0到9999之间的随机数
        String jmNo = "JM" + dateStr + String.format("%04d", randomNumber); // 格式化为四位数字
        model.addAttribute("jmNo", jmNo);
        //项目列表
        model.addAttribute("projList", modelOarsService.getProjectList());
        //桨模实体
        ModelOars modelOars = new ModelOars();
        model.addAttribute("modelOars", modelOars);
        return jumpPage("sypq/modelOars/modelOarsEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelOars/update/{id}")
    @RequiresPermissions("modelOars:edit")
    public String modelOarsUpdate(Model model, @PathVariable("id") String id){
        //附件信息
        model.addAttribute("attachs", modelOarsService.getModelShipAttachsByOarsId(id, "2"));
        //桨模信息
        model.addAttribute("modelOars", modelOarsService.findModelOarsById(id));
        //项目列表
        model.addAttribute("projList", modelOarsService.getProjectList());
        return jumpPage("sypq/modelOars/modelOarsEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelOars/finish/{id}")
    @RequiresPermissions("modelOars:finish")
    public String modelOarsFinish(Model model, @PathVariable("id") String id){
        //附件信息
        model.addAttribute("attachs", modelOarsService.getModelShipAttachsByOarsId(id, "5"));
        //桨模信息
        model.addAttribute("modelOars", modelOarsService.findModelOarsById(id));
        return jumpPage("sypq/modelOars/modelOarsFinish");
    }

    /**
     * 根据桨模code获取桨模信息
     * @param code
     * @return
     */
    @GetMapping("modelOars/getModelOarsByCode/{code}")
    @ResponseBody
    public FebsResponse getModelOarsByCode(@PathVariable("code") String code){
        return success(modelOarsService.getModelOarsByCode(code));
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "modelOars/detail/{id}")
    @RequiresPermissions("modelOars:view")
    public String modelOarsDetail(Model model, @PathVariable("id") String id){
        //附件信息
        model.addAttribute("attachs", modelOarsService.getModelShipAttachsByOarsId(id, ""));
        //桨模信息
        model.addAttribute("modelOars", modelOarsService.findModelOarsById(id));
        return jumpPage("sypq/modelOars/modelOarsDetail");
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @GetMapping("modelOars")
    @ResponseBody
    @RequiresPermissions("modelOars:view")
    public FebsResponse getAllModelOarss(ModelOars modelOars) {
        try {
            return success(modelOarsService.findModelOarsList(modelOars));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @GetMapping("modelOars/list")
    @ResponseBody
    @RequiresPermissions("modelOars:view")
    public FebsResponse modelOarsList(QueryRequest request, @Param("demondFinishTime") String demondFinishTime, @Param("name") String name, @Param("statusSearch") String statusSearch, @Param("oarsCodeSearch") String oarsCodeSearch) {
        try {
            Map<String, Object> dataTable = getDataTable(this.modelOarsService.findModelOarsPage(request, demondFinishTime, name, statusSearch, oarsCodeSearch));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 导出
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @GetMapping("modelOars/export")
    @ResponseBody
    @RequiresPermissions("modelOars:view")
    public void export(QueryRequest request,
                       @Param("demondFinishTime") String demondFinishTime,
                       @Param("name") String name,
                       @Param("statusSearch") String statusSearch,
                       @Param("oarsCodeSearch") String oarsCodeSearch,
                       HttpServletResponse response) throws FebsException {
        try {
            List<ModelOars> list = this.modelOarsService.export(request, demondFinishTime, name, statusSearch, oarsCodeSearch);
            ExcelKit.$Export(ModelOars.class, response).downXlsx(list, false);
        }catch (Exception ex){
            String message = "导出Excel失败";
            log.error(message, ex);
            throw new FebsException(message);
        }
    }

    /**
     * 新增 ModelOars
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @Log("新增ModelOars")
    @PostMapping("modelOars")
    @ResponseBody
    @RequiresPermissions("modelOars:add")
    public FebsResponse addModelOars(@Valid @RequestBody OarsAddParams params) throws FebsException {
        try {
            this.modelOarsService.createModelOars(params);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 ModelOars
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @Log("逻辑删除ModelOars")
    @GetMapping("modelOars/delete/{id}")
    @ResponseBody
    @RequiresPermissions("modelOars:delete")
    public FebsResponse deleteModelOars(@PathVariable("id") String modelOarsIds) throws FebsException {
        try {
            ModelOars modelOars = new ModelOars();
            modelOars.setDeletedBy(getCurrentUser().getUserName());
            modelOars.setDeletedTime(new Date());
            modelOars.setDeletedFlg(FebsConstant.INVALID);
            this.modelOarsService.deleteModelOars(modelOars,modelOarsIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 ModelOars
    * @author ganjun
    * @date 2024-11-18 22:28:43
    */
    @Log("物理删除ModelOars")
    @GetMapping("modelOars/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("modelOars:delete")
    public FebsResponse physicallyDeleteModelOars(@PathVariable("id") String modelOarsIds) throws FebsException {
        try {
            this.modelOarsService.physicallyDeleteModelOars(modelOarsIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 ModelOars
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @Log("修改ModelOars")
    @PostMapping("modelOars/update")
    @ResponseBody
    @RequiresPermissions("modelOars:edit")
    public FebsResponse updateModelOars(@Valid @RequestBody OarsAddParams params) throws FebsException {
        try {
            this.modelOarsService.updateModelOars(params);
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

    /**
     * 完成 ModelOars
     * @author ganjun
     * @date 2024-11-18 22:28:43
     */
    @Log("完成ModelOars")
    @PostMapping("modelOars/finish")
    @ResponseBody
    @RequiresPermissions("modelOars:finish")
    public FebsResponse finishModelOars(@Valid @RequestBody OarsAddParams params) throws FebsException {
        try {
            this.modelOarsService.finishModelOars(params);
            return success();
        } catch (Exception ex) {
            return failure(ex,"完成操作失败");
        }
    }

}
